package cn.hls.web.controller.biz;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.Car;
import cn.hls.entity.Member;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.CarCategoryService;
import cn.hls.service.CarService;
import cn.hls.service.MemberService;
import cn.hls.web.controller.base.BaseController;

/**
 * @fileName: CarController.java
 * @package: cn.hls.web.controller.biz
 * @Description: 车辆基本信息COntroller
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date: 2015-7-18
 */
@Controller
@RequestMapping("/car")
public class CarController extends BaseController<Car> {
	@Resource(name = "carService")
	private CarService carService;
	 
	@Resource(name = "carCategoryService")
	private CarCategoryService carCategorySerivce;
	@Resource(name="memberService")
	private MemberService memberService;
	/**
	 * 表单
	 * 
	 * @param id
	 * @param show
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/info")
	public ModelAndView info(Long id, boolean show,boolean print )
			throws Exception {
		Car entity = null;
		Member driver=null;
		Member escort=null;
		ModelAndView modelAndView=new ModelAndView();
		if (id != null) {
			entity = carService.find(id);
		}
		if(entity!=null){
			driver=entity.getDriver();
			escort=entity.getEscort();
		}
		modelAndView.addObject("entity",entity).addObject("driver",driver).addObject("escort",escort);
		if(show){
			modelAndView.setViewName("car/show");
		}else{
			if(print){
				modelAndView.setViewName("car/print");
			}else{
				modelAndView.addObject("carCategoryList",carCategorySerivce.loadCarCategoryInfo(null));
				modelAndView.setViewName("car/info");
			}
		}
		return modelAndView;
	}

	/**
	 * 添加操作
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Msg add(Car entity) throws Exception {
		if(!carService.exists(entity)){
			boolean flag = carService.add(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(201);
			}
		}else{
			msg.setInfo("存在相同的车牌信息!");
			msg.setStatus(201);
		}
		return msg;
	}

	 
	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/del")
	public Msg del(String ids) throws Exception {
		String[] tempIds = ids.split("\\,");
		carService.delete(tempIds);
		return msg;
	}

	/**
	 * 修改操作
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Msg update(Car entity) throws Exception {
		try {
			boolean flag = carService.update(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(201);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setInfo(e.getMessage());

		}
		return msg;
	}

	/**
	 * 分页查询
	 * 
	 * @param entity 查询的会友对象
	 * @param page 会友分页对象
	 * @return PageInfo<Car>
	 * @throws Exception
	 */
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<Car> list(Car entity, PageInfo<Car> page)
			throws Exception {
		return carService.loadCarInfo(entity, page);
	}
	
	@RequestMapping("/loadCar")
	@ResponseBody
	public List<Car> loadCar(Car entity)throws Exception{
		return carService.loadCarList(entity);
	}
	
	@RequestMapping("/view")
	public ModelAndView view(HttpServletRequest request) {
		return new ModelAndView("/car/list").addObject("searchAttribute",request.getParameter("pa"));

	}
  
	/**
	 * 车辆档案视图
	 * @param request
	 * @return
	 */
	@RequestMapping("/attachView")
	public ModelAndView attachView(HttpServletRequest request){
		return new ModelAndView("/carAttach/car_list");
	}
}
